﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ResumeHelper
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            UpdateLayout();
        }

        private void UpdateLayout()
        {
            pnlDrag.Top = 0;
            pnlDrag.Height = pnlContent.Height;

            pnlWebSite.Left = 0;
            pnlWebSite.Top = 0;
            pnlWebSite.Width = pnlDrag.Left;
            pnlWebSite.Height = pnlContent.Height;

            wb.Left = pnlDrag.Right;
            wb.Top = 0;
            wb.Width = pnlContent.Width - wb.Left;
            wb.Height = pnlContent.Height;
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            UpdateLayout();
        }

        private Point MouseDownPoint = new Point();
        private bool MouseDownFlg = false;
        private int oldLeft = 0;
        private void pnlDrag_MouseDown(object sender, MouseEventArgs e)
        {
            MouseDownPoint = Control.MousePosition;
            oldLeft = pnlDrag.Left;
            MouseDownFlg = true;
        }

        private void pnlDrag_MouseUp(object sender, MouseEventArgs e)
        {
            MouseDownFlg = false;
            UpdateLayout();
        }

        private void pnlDrag_MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseDownFlg == true)
            {
                Point pt = Control.MousePosition;
                 
                int left = oldLeft + (pt.X - MouseDownPoint.X);
                left = Math.Max(180, Math.Min(left, 380));
                pnlDrag.Left = left;
                
            }
        }

        private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

            if (e.Url.Host.Trim() == "")
            {
                WebSiteProcessWorker.Process(wb, wb.Document.Url);
            }
            else
            {
                WebSiteProcessWorker.Process(wb, e.Url);
            }
            
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            LoadTree();
        }

        private TreeNode _SearchNode = null;

        public TreeNode SearchNode
        {
            get { return _SearchNode; }
            set { _SearchNode = value; }
        }

        private void LoadTree()
        {
            tvWebSite.Nodes.Clear();
            SearchNode = tvWebSite.Nodes.Add("search","检索结果（0）");

            foreach (string str in WebSiteProcessWorker.WSProcessInfos.Item.Keys)
            {
                WebSiteProcessInfos.WebSiteProcessInfo info = WebSiteProcessWorker.WSProcessInfos.GetInfo(str);
                TreeNode node = null;

                if (tvWebSite.Nodes.ContainsKey(info.SiteType) == false)
                {
                    node = tvWebSite.Nodes.Add(info.SiteType, info.SiteType);
                }
                
                node = tvWebSite.Nodes[info.SiteType].Nodes.Add(info.DisplayName);
                node.ToolTipText ="网站主页：" + info.Host;
                node.Tag = info;
            }
        }

        private void tvWebSite_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Tag == null)
            {
                return;
            }
            WebSiteProcessInfos.WebSiteProcessInfo info = e.Node.Tag as WebSiteProcessInfos.WebSiteProcessInfo;

            labStatus.Text = "正在加载" + info.Host + "网站的页面自动处理程序，请稍候..." ;

            if (WebSiteProcessWorker.WBPProcessList.ContainsKey(info.Host) == false)
            {
                WebSiteProcessWorker.LoadWebSiteProcess(info.Host);
            }

            labStatus.Text = "页面自动处理程序加载成功，正在跳转到招聘页面：" + info.Url;

            wb.Navigate("about:blank");
            wb.Navigate(info.Url);
        }
    }
}
